package com.ethan.cj.ssm.controller;

import com.ethan.cj.ssm.entity.Mechanism;
import com.ethan.cj.ssm.service.MechanismService;
import com.ethan.cj.ssm.util.JsonUtil;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;


@Controller
@Log4j
@RequestMapping("mechanism")
public class MechanismController {

    @Autowired
    private MechanismService mechanismService;

    /**
     * 进入机构主界面
     * @return
     */
    @RequestMapping("list")
    public String list() {
        return "ces";
    }

    /**
     * 添加页面
     * @return
     */
    @RequestMapping("add")
    public String add(){
        return "addMechanism";
    }

    /**
     * @return 路径
     */
    @RequestMapping("/getMechanismList")
    @ResponseBody
    public Map<Object, Object> getMechanismList(@RequestParam("page") int page, @RequestParam("limit") int limit, @RequestParam("mname") String paramString) {
        Map map = JsonUtil.parseObject(paramString, Map.class);
        return mechanismService.getMechanismList(page, limit, map);
    }

    /**
     * 添加机构信息
     *
     * @param mechanismJson 机构JSON数据
     * @return 返回结果JSON数据
     */
    @RequestMapping("/addMechanism")
    @ResponseBody
    public Map<Object, Object> addMechanism(@RequestParam("mechanism") String mechanismJson) {
        return mechanismService.addMechanism(mechanismJson);
    }

    /**
     * 删除方法
     */
    @RequestMapping("/deleteMechanism")
    @ResponseBody
    public Map<Object, Object> deleteMechanism(@RequestParam("mid") int mid) {
        return mechanismService.deleteMechanism(mid);
    }

    /**
     * 根据ID查询某条机构信息
     * @param mid
     * @return
     */
    @RequestMapping("selectMechanismMid")
    public String selectMechanismMid(@RequestParam("mid") int mid, Model model){
        System.out.println("*******************"+mid);
        Mechanism mechanism = mechanismService.selectMechanismMid(mid);
        model.addAttribute("m",mechanism);
        return "updateMechanism";
    }

    /**
     * 修改一条信息
     * @param mechanismJson
     * @return
     */
    @RequestMapping("updateMechanism")
    @ResponseBody
    public Map<Object, Object> updateMechanism(@RequestParam("mechanism") String mechanismJson){
        return mechanismService.updateMechanism(mechanismJson);
    }
}